"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 修改目录权限后，重启数据库，数据库启动失败
Create At   : 2020/06/30
@zou_jialiang0505328126
Description :
    1. 删除pg_xlog权限
    2. 重启数据库
Expect      :
    1. 修改pg_xlog权限成功
    2. pg_xlog权限删除后重启数据库失败
History     :
    modified: 2020/12/10  DTS2020102106SHDEP1N00非问题关闭，stop失败属于正常，删除对stop的校验
"""

import unittest
import time
import sys
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common


class ModifyPgxlogPermi(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case045.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.PG_XLOG_PATH = macro.PG_XLOG_PATH
        self.COREFILE_PATH = macro.COREFILE_PATH
        self.com = Common()

    def test_modify_pgxlog_permission(self):
        # delete pg_log permission
        self.log.info("delete pg_log permission")
        modfiy_permis_cmd = 'chmod -R 000 ' + self.PG_XLOG_PATH
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.log.info(modfiy_permis_cmd)
        self.log.info(modify_msg)

        # restart opengauss fail
        is_stop = self.sh.stopDbCluster()
        self.log.info(is_stop)
        is_started = self.sh.startDbCluster()
        self.assertFalse(is_started)

    def tearDown(self):
        self.log.info('this is tearDown')
        # recover pg_log permission
        self.log.info('recover pg_log permission')

        modfiy_permis_cmd = 'chmod -R 700 ' + self.PG_XLOG_PATH
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.log.info(modfiy_permis_cmd)
        self.log.info(modify_msg)
        time.sleep(5)

        is_started = self.sh.startDbCluster()
        self.log.info(is_started)
